package com.df.bpm.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.df.bpm.dto.*;
import com.df.bpm.entity.*;
import com.df.bpm.service.impl.*;
import com.df.common.entity.Pagination;
import com.df.common.utils.CommonUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


/**
 * 需求申请单控制器
 */
@Slf4j
@Tag(name = "需求申请单控制器")
@RequestMapping("/requirement")
@RequiredArgsConstructor
@RestController
@SaCheckLogin
public class RequirementController {
    private final DtoRequirementServiceImpl dtoRequirementServiceImpl;
    private final RequirementServiceImpl requirementServiceImpl;

    /**
     * 查询需求
     * @param pagination 分页信息
     * @param temp 需求DTO线索
     * @return  需求DTO列表
     */
    @Operation(summary = "查询需求", description = "查询需求")
    @GetMapping("dtoRequirement")
    @SaIgnore
    public ResponseEntity<?> getDtoRequirement(Pagination pagination, DtoRequirement temp) {
        if (CommonUtils.varIsNotBlank(pagination.getPageNum(), pagination.getPageSize())) {
            PageHelper.startPage(pagination.getPageNum(), pagination.getPageSize());
            return ResponseEntity.ok(new PageInfo<>(dtoRequirementServiceImpl.getList4Like(temp)));
        } else {

            return ResponseEntity.ok(dtoRequirementServiceImpl.getList4Like(temp));
        }
    }

    /**
     * 查询需求详情
     * @param id 需求申请单id
     * @return  需求详情DtoRequirementDetails
     */
    @Operation(summary = "查询需求", description = "查询需求")
    @GetMapping("dtoRequirementDetails/id")
    @SaIgnore
    public ResponseEntity<?> getDtoRequirementDetails(Integer id) {
        return ResponseEntity.ok(dtoRequirementServiceImpl.getDtoRequirementDetails(id));
    }

    /**
     * 更新申请单详情关系
     * @param temp   申请单详情
     * @return  0
     */
    @Operation(summary = "更新申请单详情关系", description = "更新申请单详情关系")
    @PutMapping("relations/dtoRequirementDetails")
    @SaIgnore
    public ResponseEntity<?> updateRelationsByDtoRequirementDetails(
            @RequestBody DtoRequirementDetails temp) {
        dtoRequirementServiceImpl.updateRelationsByDtoRequirementDetails(temp);
        return ResponseEntity.ok(0);
    }

    /**
     * 保存申请单详情
     * @param temp   申请单详情
     * @return  0
     */
    @Operation(summary = "保存申请单详情", description = "保存申请单详情")
    @PostMapping("dtoRequirementDetails")
    @SaIgnore
    public ResponseEntity<?> saveDtoRequirementDetails(
            @RequestBody DtoRequirementDetails temp) {
        dtoRequirementServiceImpl.saveDtoRequirementDetails(temp);
        return ResponseEntity.ok(0);
    }

    /**
     * 新增需求申请单
     * @param temp   需求申请单
     * @return  新增成功后的需求申请单
     */
    @Operation(summary = "新增需求申请单", description = "新增需求申请单")
    @PostMapping("")
    @SaIgnore
    public ResponseEntity<?> newRequirement(@RequestBody Requirement temp) {
        requirementServiceImpl.add(temp);
        return ResponseEntity.ok(temp);
    }

    /**
     * 查询需求申请单
     * @param pagination 分页信息
     * @param temp 需求申请单线索
     * @return  需求申请单列表
     */
    @Operation(summary = "查询需求申请单", description = "查询需求申请单")
    @GetMapping("")
    @SaIgnore
    public ResponseEntity<?> getRequirement(Pagination pagination, Requirement temp) {
        if (CommonUtils.varIsNotBlank(pagination.getPageNum(), pagination.getPageSize())) {
            PageHelper.startPage(pagination.getPageNum(), pagination.getPageSize());
            return ResponseEntity.ok(new PageInfo<>(requirementServiceImpl.getList4Like(temp)));
        } else {
            return ResponseEntity.ok(requirementServiceImpl.getList4Like(temp));
        }
    }

    /**
     * 更改需求申请单
     * <p>目前建议只改价值主张</p>
     * @param temp   更改申请单
     * @return  更改成功后的需求申请单
     */
    @Operation(summary = "更改需求申请单", description = "更改需求申请单")
    @PutMapping("")
    @SaIgnore
    public ResponseEntity<?> updateRequirement(@RequestBody Requirement temp) {
        requirementServiceImpl.update(temp);
        return ResponseEntity.ok(temp);
    }

}
